package com.tencent50.easy;

/**
 * @author zhengTao
 * @version 1.0
 * @description: 环形链表
 * @date 2022/7/24 15:26
 */
public class Demo12 {
    public boolean hasCycle(ListNode head) {
        if(head==null||head.next==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head.next;
        //while循环判断必须保证fast和fast.next不为空
        while (fast!=null&&fast.next!=null){
            if(fast==slow){
                return true;
            }
            fast=fast.next.next;  //可能为空了
            slow=slow.next;
        }
        return false;
    }

     class ListNode {
         int val;
         ListNode next;
         ListNode(int x) {
             val = x;
             next = null;
         }
     }

}
